package com.sanglh.tutorial.settings;

import java.lang.reflect.Method;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.sanglh.tutorial.R;
import com.sanglh.tutorial.common.base.BaseListAdapter;
import com.sanglh.tutorial.common.base.Logging;
import com.sanglh.tutorial.common.util.ComponentUtils;
import com.sanglh.tutorial.common.util.SettingsUtils;
import com.sanglh.tutorial.main.MainActivity;

public class SettingsActivity extends ListActivity implements
		OnItemClickListener {
	private ArrayList<SettingsEntry> mEntries = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		loadData();
	}

	// =================================================================================
	private void loadData() {
		mEntries.add(new SettingsEntry("Mobile Data", "onMobileData"));
		mEntries.add(new SettingsEntry("GPS", "onGPS"));
		mEntries.add(new SettingsEntry("Brightness", "onBrightness"));
		mEntries.add(new SettingsEntry("WIFI", "onWifi"));
		mEntries.add(new SettingsEntry("Bluetooth", "onBluetooth"));
		mEntries.add(new SettingsEntry("Ring Mode", "onRingMode"));
		mEntries.add(new SettingsEntry("Airplane Mode", "onAirPlaneMode"));
		mEntries.add(new SettingsEntry("Hide icon", "onHideIcon"));
		BaseListAdapter<SettingsEntry> adapter = new BaseListAdapter<SettingsEntry>(
				this, mEntries, R.layout.row_base);
		getListView().setAdapter(adapter);
		getListView().setOnItemClickListener(this);
	}

	// =================================================================================
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		SettingsEntry entry = mEntries.get(position);
		entry.enabled = !entry.enabled;
		Logging.log(entry.method + " " + entry.enabled);

		try {
			Method method = SettingsActivity.class.getMethod(entry.method,
					Integer.TYPE);
			method.setAccessible(true);
			method.invoke(SettingsActivity.this, position);
		} catch (Exception e) {
			Logging.log(e);
		}
	}

	// =================================================================================
	public void onMobileData(int position) {

		SettingsUtils.setMobileDataEnabled(SettingsActivity.this,
				mEntries.get(position).enabled);
	}

	public void onGPS(int position) {
		SettingsUtils.toggleGPS(SettingsActivity.this);
	}

	// =================================================================================
	public void onBrightness(int position) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setCancelable(true);
		View rootView = LayoutInflater.from(this).inflate(
				R.layout.view_brightness, null);
		builder.setView(rootView);
		final CheckBox cb = (CheckBox) rootView
				.findViewById(R.id.cb_brightmode);

		cb.setChecked(SettingsUtils.getBrightnessMode(this) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				SettingsUtils.setBrightMode(SettingsActivity.this, isChecked);

			}
		});
		SeekBar sb = (SeekBar) rootView.findViewById(R.id.sb_brightness);
		sb.setProgress(SettingsUtils.getBrightness(SettingsActivity.this));
		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				cb.setChecked(false);
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (fromUser) {
					Logging.log("progress " + progress);
					SettingsUtils
							.setBrightness(SettingsActivity.this, progress);
				}

			}
		});
		AlertDialog alertDialog = builder.create();
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.setCancelable(true);
		alertDialog.show();

	}

	// =================================================================================
	public void onWifi(int position) {
		SettingsUtils.setWifiEnabled(this, mEntries.get(position).enabled);
	}

	// =================================================================================
	public void onBluetooth(int position) {
		if (SettingsUtils.isBluetoothSupported()) {
			SettingsUtils.setBluetoothEnabled(this,
					mEntries.get(position).enabled);
		}
	}

	// =================================================================================
	public void onRingMode(int position) {
		SettingsUtils.switchSoundMode(this);
	}

	// =================================================================================
	public void onAirPlaneMode(int position) {
		SettingsUtils.setAirPlaneModeEnable(this,
				mEntries.get(position).enabled);
	}

	// =================================================================================
	public void onHideIcon(int position) {
		ComponentUtils.disableComponent(this, MainActivity.class);
	}

}
